package com.msun.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.msun.dao.FoodDao;
import com.msun.entity.PageResult;
import com.msun.entity.QueryPageBean;
import com.msun.pojo.Food;
import com.msun.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = FoodService.class)
@Transactional
public class FoodServiceImpl implements FoodService {
    @Autowired
    private FoodDao foodDao;

    @Override
    public void add(List<Food> list) {

        for (Food food : list) {
            Integer id = food.getId();

        Long count= foodDao.findById(id);

        if(count > 0){
            foodDao.edit(food);
        }else {
            foodDao.add(food);
        }
        }

    }

    @Override
    public PageResult findAll(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Food> page=   foodDao.findAll(queryString);
        long total = page.getTotal();
        List<Food> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    public void addFood(Food food) {
        foodDao.add(food);
    }

    @Override
    public List<String> findFoodById(Integer id) {
       List<String> list= foodDao.findFoodById(id);
        return list;
    }

    @Override
    public List<Food> findAllFoods(Integer id) {
        List<Food> list= foodDao.findAllFoods(id);
        return list;
    }

    @Override
    public List<Integer> findFoodIdsByOrderId(Integer id) {
        List<Integer> list=  foodDao.findFoodIdsByOrderId(id);
        return list;
    }

    @Override
    public void edit(Food food) {
        foodDao.edit(food);
    }

    @Override
    public void deleteById(Integer id) {
        foodDao.deleteById(id);
    }
}
